﻿#ifndef IMAGECAMERA_H
#define IMAGECAMERA_H

#include <QObject>
#include <QPixmap>
#include <QImage>
#include <QThread>
#include <QMutex>
#include <QWaitCondition>

//#define IMAGE_WIDTH 1280
//#define IMAGE_HEIGHT 960
//#define IMAGE_SIZE (IMAGE_WIDTH * IMAGE_HEIGHT * 2)
//#define BUFFER_COUNT 5


class ImageCamera : public QThread
{
    Q_OBJECT
public:
    explicit ImageCamera(QString name, QObject *parent = 0);
    ~ImageCamera();

public:
    bool initCamera();              //  初始化相机
    bool startCapture();            //  开始抓图
    void stopCapture();             //  停止抓图
    void releaseCamera();           //  释放相机
    void setImageSize(int width,int high);
    QString errorString();

    void pause();
    void resume();
    void exitThread();

private:
    void run();
    int grabImage(quint8 * buf, int* imgLen);        //  实际的抓图函数
    void yuyv_to_rgb888(quint8* yuv,quint8* rgb, int imgWidth, int imgHeight);

signals:
    void image(QImage);

private:
    QString cameraName;         //  相机的设备节点
    QString err;
    int nWidth;                 //  图片宽、高
    int nHeight;
    int nSize;

    char pix_format[64];        //  当前相机的图片格式
    int cam_fd;                 //  相机描述符
    int buffer_count;           //  相机抓图容器数量
    quint8** video_buffer_ptr;      //  相机抓图容器队列指针
    QMutex sync;
    QMutex exitMutex;
    QWaitCondition pauseCond;
    bool isExit,isPause;
};

#endif  // IMAGECAMERA_H
